/* 
 *	Copyright 2014  Yanqing Wu
 *		email: yqwu_yqwu@126.com
 *
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
*/

#include "stdafx.h"
#include "StdCtrlSkinBg.h"
#include "StdCtrlSkinTool_Utils.h"

#include "StdCtrlSkinWnd.h"


void CStdCtrlSkinBg::InitDataElt(CStdCtrlSkinWnd* pMgr)
{
	m_backcolor = RGB(0xff, 0xff, 0xff);
	m_pWnd = pMgr;
}

CStdCtrlSkinBg::CStdCtrlSkinBg(void)
{
	InitDataElt(nullptr);
}
CStdCtrlSkinBg::CStdCtrlSkinBg(CComPtr<IXMLDOMNode> spNode, CStdCtrlSkinWnd* pWnd)
{
	InitDataElt(pWnd);
	LoadXmlNode(spNode);
}

CStdCtrlSkinBg::~CStdCtrlSkinBg(void)
{
	for(auto i : m_table_list)
		delete i.second;
}

CString CStdCtrlSkinBg::GetFilePath(void)
{
	if ( m_pWnd )
		return m_pWnd->GetFilePath();
	return _T("");
}

bool CStdCtrlSkinBg::LoadXmlNode(CComPtr<IXMLDOMNode> spNode)
{
	//	Load type
	auto attribPairs = StdCtrlSkinTool_XmlGetAttribPairs(spNode);
	CStdCtrlSkin_EnumAttrib attrib;
	auto pEnum = StdCtrlSkinTool_GetEnum();
	for(auto i : attribPairs)
	{
		auto name = i.first;
		auto value = i.second;
		auto correct = pEnum->Parse(attrib, name);
		if ( !correct )
		{
			SetCustomAttrib(name, value);
			continue;
		}
		switch(attrib)
		{
		case CStdCtrlSkin_EnumAttrib::BACKCOLOR:
			m_backcolor = StdCtrlSkinTool_CString2Long(value);
			break;
		default:
			SetCustomAttrib(name, value);
			break;
		}
	}
	//	Load subnode for gradiant
	CComPtr<IXMLDOMNodeList> spChildren;
	spNode->get_childNodes(&spChildren);
	long length;
	spChildren->get_length(&length);
	CStdCtrlSkinTable* pTable;
	int count = 0;
	for(long i=0; i!=length; ++i)
	{
		CComPtr<IXMLDOMNode> spChild;
		spChildren->get_item(i, &spChild);
		auto name = StdCtrlSkinTool_XmlGetNodeName(spChild);
		CStdCtrlSkin_EnumSubNode type;
		pEnum->Parse(type, name);
		switch(type)
		{
		case CStdCtrlSkin_EnumSubNode::TABLE:
			pTable = new CStdCtrlSkinTable(spChild, this);
			m_table_list[count] = pTable;
			count ++;
			break;
		default:
			break;
		}
	}
	return true;
}

HBITMAP CStdCtrlSkinBg::GenerateBitmap(CSize size, HDC hDC)
{
	CRect rect;
	rect.left = 0;
	rect.right = size.cx;
	rect.top = 0;
	rect.bottom = size.cy;
	SetRect(rect);

	HDC hMemDC = CreateCompatibleDC(hDC);
	HBITMAP hBmp = CreateCompatibleBitmap(hDC, size.cx, size.cy);
	HBITMAP hOld = (HBITMAP)SelectObject(hMemDC, hBmp);

	HBRUSH hBrush = CreateSolidBrush(m_backcolor);
	HPEN hPen = (HPEN)GetStockObject(NULL_PEN);
	HBRUSH hOldBrush = (HBRUSH)SelectObject(hMemDC, hBrush);
	HPEN hOldPen = (HPEN)SelectObject(hMemDC, hPen);
	Rectangle(hMemDC, rect.left, rect.top, rect.right+1, rect.bottom+1);
	SelectObject(hMemDC, hOldBrush);
	SelectObject(hMemDC, hOldPen);
	DeleteObject(hBrush);

	for(auto i : m_table_list)
		i.second->Draw(hMemDC);
	SelectObject(hMemDC, hOld);
	DeleteDC(hMemDC);
	return hBmp;
}

void CStdCtrlSkinBg::SetBackColor(COLORREF color)
{
	m_backcolor = color;
}

#ifdef __DUMPTEST
void CStdCtrlSkinBg::Dump(std::ofstream& fio, CStdCtrlSkin_EnumDump type)
{
	if ( !((int)type & (int)CStdCtrlSkin_EnumDump::BG) )
		return;
	CString colorS;
	colorS.Format(_T("%x"), m_backcolor);
	CStringA colorA(colorS);

	fio << "Background info backcolor=" << colorA.GetString() << std::endl;
	for(auto i : m_table_list)
		i.second->Dump(fio, type);
}
#endif